package com.xft.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.xft.entity.User;
import com.xft.service.UserService;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xft 创建日期为:2018年2月9日
 */
@RestController
public class UserController {

    @Resource
    UserService userService;

    @RequestMapping("/")
    public String index() {
        return "redirect:/list";
    }

    @RequestMapping("/list")

    public List<User> list(Model model) {
        List<User> users = userService.getUserList();
//        model.addAttribute("users", users);
//        return "user/list";
        return users;
    }

    @RequestMapping("/toAdd")
    public String toAdd() {
        return "user/userAdd";
    }


    @PostMapping("/add")
    @ResponseBody
    public String add(@RequestBody User user) {
//        Map<String, Object> modelMap = new HashMap<String, Object>();
        userService.save(user);
//        modelMap.put("success", true);
        return "success";
//        return "redirect:/list";
    }

    @RequestMapping("/toEdit")
    public String toEdit(Model model,Long id) {
        User user = userService.findUserById(id);
        model.addAttribute("user", user);
        return "user/userEdit";
    }

    @PutMapping("/edit")
    public String edit(@RequestBody User user) {
        userService.edit(user);
        return "success";
//        return "redirect:/list";
    }

    @DeleteMapping(value = "/delete/{id}")
    public String delete(@PathVariable Long id) {
        userService.delete(id);
        return "success";
//        return "redirect:/list";
    }
}
